package com.dingreading.cloud.admin.service.impl;

import com.dingreading.cloud.admin.entity.WhPurchase;
import com.dingreading.cloud.admin.entity.table.WhPurchaseTableDef;
import com.dingreading.cloud.admin.mapper.WhPurchaseMapper;
import com.dingreading.cloud.admin.service.WhPurchaseService;
import com.dingreading.cloud.common.util.EnumType;
import com.dingreading.cloud.common.util.PageDto;
import com.dingreading.cloud.common.util.PageUtil;
import com.google.common.collect.Lists;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.update.UpdateChain;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 服务层实现。
 *
 * @author Administrator
 * @since 2023-11-22
 */
@Service
public class WhPurchaseServiceImpl extends BaseServiceImpl<WhPurchaseMapper, WhPurchase, WhPurchaseTableDef> implements WhPurchaseService {

    @Override
    protected WhPurchaseTableDef getTable() {
        return WhPurchaseTableDef.whPurchase;
    }

    @Resource
    WhPurchaseMapper purchaseMapper;

    @Override
    public PageDto<WhPurchase> pageListRealNum(PageUtil pageUtil, String keyword, Integer status) {
        //设置其他参数
        Map<String, Object> otherParams = new HashMap<>();
        otherParams.put("keyword", keyword);
        otherParams.put("status", status);

        Page<WhPurchase> page = getMapper().xmlPaginate("pageListRealNumXml", Page.of(pageUtil.getPage(), pageUtil.getRows()), otherParams);

        long count = page.getTotalRow();
        List<WhPurchase> list = page.getRecords();

        PageDto<WhPurchase> data = new PageDto<>();
        data.setTotal(count);
        data.setList(list);

        return data;
    }

    @Override
    public WhPurchase getByCode(String code) {
        List<WhPurchase> list = purchaseMapper.getByCode(code);
        if (CollectionUtils.isNotEmpty(list)) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public WhPurchase getBeanByCode(String code) {
        return getMapper().selectOneByCondition(table.code.eq(code));
    }

    @Override
    public int dropData(String code) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.code.eq(code))
                .and(table.status.in(Lists.newArrayList(EnumType.Purchase.SAVE.getType(),
                        EnumType.Purchase.CANCEL.getType(), EnumType.Purchase.STEP_1.getType())));
        return getMapper().deleteByCondition(condition);
    }

    @Override
    public boolean cancel(String code, Long operatorId, String operator, String operateRemark, Date time) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.code.eq(code))
                .and(table.status.in(Lists.newArrayList(EnumType.Purchase.SAVE.getType(), EnumType.Purchase.STEP_1.getType())));

        return UpdateChain.of(WhPurchase.class)
                .set(table.status, EnumType.Purchase.CANCEL.getType())
                .set(table.statusName, EnumType.Purchase.CANCEL.getName())
                .set(table.operatorId, operatorId)
                .set(table.operator, operator)
                .set(table.operateRemark, operateRemark)
                .set(table.operatorTime, time)
                .where(condition)
                .update();
    }

    @Override
    public boolean cancel(String code, String operatorUid, String operator, String operateRemark, Date time) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.code.eq(code))
                .and(table.status.in(Lists.newArrayList(EnumType.Purchase.SAVE.getType(), EnumType.Purchase.STEP_1.getType())));

        return UpdateChain.of(WhPurchase.class)
                .set(table.status, EnumType.Purchase.CANCEL.getType())
                .set(table.statusName, EnumType.Purchase.CANCEL.getName())
                .set(table.operatorUid, operatorUid)
                .set(table.operator, operator)
                .set(table.operateRemark, operateRemark)
                .set(table.operatorTime, time)
                .where(condition)
                .update();
    }

    @Override
    public boolean updateWaitIn(String code) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.code.eq(code))
                .and(table.status.eq(EnumType.Purchase.STEP_2.getType()));

        return UpdateChain.of(WhPurchase.class)
                .set(table.status, EnumType.Purchase.STEP_1.getType())
                .set(table.statusName, EnumType.Purchase.STEP_1.getName())
                .where(condition)
                .update();
    }

    @Override
    public boolean updateEntering(String code) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.code.eq(code))
                .and(table.status.eq(EnumType.Purchase.STEP_1.getType()));

        return UpdateChain.of(WhPurchase.class)
                .set(table.status, EnumType.Purchase.STEP_2.getType())
                .set(table.statusName, EnumType.Purchase.STEP_2.getName())
                .where(condition)
                .update();
    }

    @Override
    public boolean setOk(String code, Long operatorId, String operator, String remark, Date time) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.code.eq(code))
                .and(table.status.eq(EnumType.Purchase.STEP_2.getType()));

        return UpdateChain.of(WhPurchase.class)
                .set(table.status, EnumType.Purchase.OK.getType())
                .set(table.statusName, EnumType.Purchase.OK.getName())
                .set(table.operatorId, operatorId)
                .set(table.operator, operator)
                .set(table.remark, remark)
                .set(table.okTime, time)
                .where(condition)
                .update();
    }

    @Override
    public boolean setOk(String code, String operatorUid, String operator, String remark, Date time) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.code.eq(code))
                .and(table.status.eq(EnumType.Purchase.STEP_2.getType()));

        return UpdateChain.of(WhPurchase.class)
                .set(table.status, EnumType.Purchase.OK.getType())
                .set(table.statusName, EnumType.Purchase.OK.getName())
                .set(table.operatorUid, operatorUid)
                .set(table.operator, operator)
//                .set(table.remark, remark)
                .set(table.okTime, time)
                .where(condition)
                .update();
    }


    @Override
    public boolean notRealSetOk(String code, String operatorUid, String operator, Date time) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.code.eq(code));

        return UpdateChain.of(WhPurchase.class)
                .set(table.status, EnumType.Purchase.OK.getType())
                .set(table.statusName, EnumType.Purchase.OK.getName())
                .set(table.operatorUid, operatorUid)
                .set(table.operator, operator)
                .set(table.okTime, time)
                .where(condition)
                .update();
    }

    @Override
    public boolean updateExpressInfo(String code, String expressInfo, Double expressPrice) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.code.eq(code));

        return UpdateChain.of(WhPurchase.class)
                .set(table.expressInfo, expressInfo)
                .set(table.expressPrice, expressPrice)
                .where(condition)
                .update();
    }

    @Override
    public long getSupplierCnt(Long supplierId) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.supplierId.eq(supplierId));

        return getMapper().selectCountByCondition(condition);
    }

}
